﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Globalization;
using ThirdServices.AlertModel.Factory.BaseInterface;
using ThirdServices.AlertModel.Factory.Entities.DTO;
using ThirdServices.WindSettings;
using ThirdServices.AlertModel.Analyzers;

namespace ThirdServices.AlertModel.Norway
{
    public class NorwayWindAlert : IAlert
    {
        private double lat;
        private double lon;
        private double radius;

        public double Lat
        {
            get { return lat; }
            set { lat = value; }
        }

        public double Lon
        {
            get { return lon; }
            set { lon = value; }
        }

        public double Radius
        {
            get { return radius; }
            set { radius = value; }
        }


        #region IAlert Members

        public List<BaseAlert> CheckAlert()
        {
            List<WindSpeedData> WindSpeedData = new List<WindSpeedData>();
            Console.WriteLine("NorwayWind : recieving information...");
            string url = AlertsLinks.NorwayWindAlertLink(lat, lon);
            XDocument xDoc = XDocument.Load(url);
            foreach (XElement xe in xDoc.Descendants("time"))
            {
                try
                {
                    //Console.WriteLine(xe.Attribute("from").Value.ToString() + "\t" + xe.Element("location").Element("windSpeed").Attribute("mps"));
                    WindSpeedData.Add(new WindSpeedData
                    {
                        Date = Convert.ToDateTime(xe.Attribute("from").Value, CultureInfo.InvariantCulture),
                        Speed = Convert.ToDouble(xe.Element("location").Element("windSpeed").Attribute("mps").Value.ToString(), CultureInfo.InvariantCulture)
                    });
                }
                catch (Exception ex) { break; }
            }
            var baseAlerts = WindAnalyser.AnalyseWindData(WindSpeedData);

            return baseAlerts;

        }

        #endregion

        public void GetAlert()
        {
        }
    }
}
